const connection = require('../app/database');

class LabelService {
    // 创建标签
    async create(name) {
        try {
            // 拼接 SQL 语句
            const statement = 'INSERT INTO `label` (name) VALUES (?);';

            // 执行 SQL 语句
            const [result] = await connection.execute(statement, [name]);

            return result;
        } catch (error) {
            console.error('LabelService - create:', error);
            throw error; // 抛出异常，让调用者处理
        }
    }

    // 根据标签名查询标签
    async queryLabelsByName(name) {
        try {
            // 拼接 SQL 语句
            const statement = 'SELECT * FROM `label` WHERE name = ?;';

            // 执行 SQL 语句
            const [result] = await connection.execute(statement, [name]);

            return result[0];
        } catch (error) {
            console.error('LabelService - queryLabelsByName:', error);
            throw error; // 抛出异常，让调用者处理
        }
    }
}

module.exports = new LabelService();
